/*
 * Copyright 2006-2009 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.osgi.service.importer.support.internal.exception;

import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.service.blueprint.container.ServiceUnavailableException;
import org.springframework.osgi.util.OsgiFilterUtils;
import org.springframework.osgi.util.OsgiServiceReferenceUtils;

/**
 * Utility class that creates either Spring DM or Blueprint exceptions. It's used for performing lazy class loading and
 * similar exception messages between the two.
 * 
 * @author Costin Leau
 */
public abstract class BlueprintExceptionFactory {

	public static RuntimeException createServiceUnavailableException(Filter filter) {
		return BlueprintFactory.createServiceUnavailableException(filter);
	}

	public static RuntimeException createServiceUnavailableException(ServiceReference reference) {
		return BlueprintFactory.createServiceUnavailableException(reference);
	}

	private static abstract class BlueprintFactory {
		private static RuntimeException createServiceUnavailableException(Filter filter) {
			return new ServiceUnavailableException("service matching filter=[" + filter + "] unavailable", filter
					.toString());
		}

		private static RuntimeException createServiceUnavailableException(ServiceReference reference) {
			String id = (reference == null ? "null" : "" + OsgiServiceReferenceUtils.getServiceId(reference));
			return new ServiceUnavailableException("service with id=[" + id + "] unavailable", OsgiFilterUtils
					.getFilter(reference));
		}
	}
}
